package vn.edu.saigontech.ncms.web.materialFacilities;

import vn.edu.saigontech.ncms.model.system.Room;
import vn.edu.saigontech.ncms.service.RoomService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateRoomAction extends AbstractValidator {
	
	private Long id;
	private Long locationId;
	private String code;
	private String name;
	private String seatCount;
	private int version;

	public String check() {
		if(Checker.isNull(this.locationId)) {
			this.sendKeyError("validator.required", "location");
		}
		if(Checker.isEmpty(this.code)) {
			this.sendKeyError("validator.required", "code");
		}
		if(Checker.isNull(this.id)) {
			if(!Checker.isNull(RoomService.INSTANCE.findByCode(this.code))) {
				this.sendKeyError("validator.exist", "code");
			}
		}
		else {
			Room room = RoomService.INSTANCE.findById(this.id);
			if(room.getVersion() != this.version) {
				this.sendKeyError("validator.concurrency", "room");
			}
			Room cRoom = RoomService.INSTANCE.findByCode(this.code);
			if(!Checker.isNull(cRoom)) {
				if(!Checker.equals(this.id, cRoom.getId())) {
					this.sendKeyError("validator.exist", "code");
				}
			}
		}
		if(Checker.isEmpty(this.name)) {
			this.sendKeyError("validator.required", "name");
		}
		if(Checker.isEmpty(this.seatCount)) {
			this.sendKeyError("validator.required", "seatCount");
		}
		if(!Checker.isInteger(this.seatCount) || !Checker.isPositive(this.seatCount)) {
			this.sendKeyError("validator.invalid", "seatCount");
		}
		return this.toSuccess();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public String getSeatCount() {
		return seatCount;
	}

	public void setSeatCount(String seatCount) {
		this.seatCount = seatCount;
	}

	public Long getLocationId() {
		return locationId;
	}

	public void setLocationId(Long locationId) {
		this.locationId = locationId;
	}
}
